using UnityEngine;
using System.Collections;

[System.Serializable]
public class PowerUp_LaserInfo
{
	
	public Texture2D[] lazerModeAnimationImgs;
	public GameObject laserGo;
	public GameObject laserGoInTheScene;
	public float laserFromPlayerXDistance;
	public float laserFromPlayerYDistance;
	public float xVel;
	public float yVel;
	private float currentVelX;
	private float currentVelY;
	private bool laserMode = false;
	private float laserModeCronometer;
	public float laserModeLifeTime;
	private GameObject playerGo;
	private PlayerMechanic playerMechanic;

	public void Awake ()
	{
		this.playerGo = GameObject.FindGameObjectWithTag (Tags.PLAYER);
		this.playerMechanic = playerGo.GetComponent<PlayerMechanic> ();
	}
	
	public void Start ()
	{
		
	}
	
	public void Update ()
	{
		if (this.laserModeCronometer < this.laserModeLifeTime) {
			if (!this.laserMode) {
				this.laserMode = true;
				this.laserGoInTheScene = MonoBehaviour.Instantiate (this.laserGo,
					new Vector3 (
					this.playerGo.transform.position.x + this.laserFromPlayerXDistance,
					this.playerGo.transform.position.y + this.laserFromPlayerYDistance,
					0), this.laserGo.transform.rotation) as GameObject;
				this.laserGoInTheScene.transform.parent = this.playerGo.transform;
			}
		
			this.currentVelX = Input.GetAxis ("Horizontal") * this.xVel;
			this.currentVelY = Input.GetAxis ("Vertical") * this.yVel;
			
			this.currentVelX *= Time.deltaTime;
			this.currentVelY *= Time.deltaTime;
		
			this.playerGo.transform.Translate(new Vector3(this.currentVelX, this.currentVelY, 0));
			
			Vector3 tmpPosition = new Vector3(playerGo.transform.position.x, playerGo.transform.position.y, playerGo.transform.position.z);
			
			if(this.playerGo.transform.position.y > playerMechanic.maxAltitude){
				tmpPosition.y = playerMechanic.maxAltitude;
			}
			
			if(this.playerGo.transform.position.y < playerMechanic.minAltitude){
				tmpPosition.y = playerMechanic.minAltitude;
			}
			
			if(this.playerGo.transform.position.x > playerMechanic.maxLongitude)
			{
				tmpPosition.x = playerMechanic.maxLongitude;
			}
			
			if(this.playerGo.transform.position.x < playerMechanic.minLongitude)
			{
				tmpPosition.x = playerMechanic.minLongitude;
			}
			
			this.playerGo.transform.position = tmpPosition;
			
			this.laserModeCronometer += Time.deltaTime;
			
		} else {
			this.laserModeCronometer = 0;
			this.laserMode = false;
			this.playerGo.renderer.material.color = Color.white;
			this.playerMechanic.ChangeMode (PlayerState.Normal);
			MonoBehaviour.Destroy (this.laserGoInTheScene);
			
			Debug.Log (" ----- Terminou Lazer Mode ----- ");
		}	
	}
	
}
